class RevisacionsController < ApplicationController

  before_filter :login_required
 
  def index
    store_location
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    store_location
    @revisacion_pages, @revisacions = paginate :revisacions, :per_page => 10
    @page, @lnk_from, @lnk_to = numeros_pagina @revisacion_pages
    @alus = Persona.find_alumnos(:all,
              :conditions=>"NOT EXISTS( SELECT * FROM revisacions r WHERE (r.persona_id=personas.id AND DATEDIFF(CURDATE(),r.fecha)<365 AND r.done=1 ) )",
              :group=>'personas.id',
              :order=>'personas.nombres') 
  end

  def show
    store_location
    @revisacion = Revisacion.find(params[:id])
  end

  def new
    @revisacion = Revisacion.new
    # preseteo el alumno segun lo que llego por params
    if params[:revisacion_alumno] != nil then 
      @revisacion.persona_id = params[:revisacion_alumno].to_i
    else
      flash[:error] = "No especific&oacute; alumno para la revisaci&oacute;n"
      redirect_back_or_default :action=>'list'
    end
  end

  def create
    @revisacion = Revisacion.new(params[:revisacion])
    if @revisacion.save
      flash[:success] = 'Revisacion was successfully created.'
      redirect_back_or_default :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @revisacion = Revisacion.find(params[:id])
  end

  def update
    @revisacion = Revisacion.find(params[:id])
    if @revisacion.update_attributes(params[:revisacion])
      flash[:success] = 'Revisacion was successfully updated.'
      redirect_back_or_default :action => 'show', :id => @revisacion
    else
      render :action => 'edit'
    end
  end

  def destroy
    Revisacion.find(params[:id]).destroy
    redirect_back_or_default :action => 'list'
  end
end
